home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / exit / exitTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-31  |  2.6 KB  |  125 lines

  1. /* 
  2.  * exitTest.c --
  3.  *
  4.  *    Test the exit system call.
  5.  *
  6.  * Copyright 1990 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header$";
  18. #endif /* not lint */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/wait.h>
  25.  
  26. extern int errno;
  27. static int errors;
  28.  
  29. #ifdef __STDC__
  30. static void testExit(int exitCode);
  31. #else
  32. static void testExit();
  33. #endif
  34.  
  35. /*
  36.  *----------------------------------------------------------------------
  37.  *
  38.  * main --
  39.  *
  40.  *    Fork off several child processes that exit with different
  41.  *      exit codes.
  42.  *
  43.  * Results:
  44.  *    Exits with a zero exit status if everything works properly,
  45.  *      non-zero otherwise.
  46.  *
  47.  * Side effects:
  48.  *    Forks several sub-processes which immediately exit.
  49.  *      Prints a message to stderr if there are any problems.
  50.  *
  51.  *----------------------------------------------------------------------
  52.  */
  53.  
  54. void
  55. main(void)
  56. {
  57.  
  58.     testExit(EXIT_SUCCESS);
  59.     testExit(EXIT_FAILURE);
  60.     testExit(99);
  61.     testExit(255);
  62.     if (errors != 0) {
  63.     exit(EXIT_FAILURE);
  64.     }
  65.     exit(EXIT_SUCCESS);
  66. }
  67.  
  68. /*
  69.  *----------------------------------------------------------------------
  70.  *
  71.  * testExit --
  72.  *
  73.  *    Fork off a child process that exits with the given exit code.
  74.  *      Wait for the child to exit.  Check the return status and make
  75.  *      sure that it matches the exit status.
  76.  *
  77.  * Results:
  78.  *    none.
  79.  *
  80.  * Side effects:
  81.  *      Prints a message to stderr nad increments `errors' if there is
  82.  *      an error.
  83.  *
  84.  *----------------------------------------------------------------------
  85.  */
  86.  
  87. static void
  88. testExit(exitCode)
  89.     int exitCode;
  90. {
  91.     int w;
  92.     int child;
  93.     union wait ws;
  94.  
  95.     switch (child = fork()) {
  96.  
  97.     case 0:
  98.     exit(exitCode);
  99.     (void) fprintf(stderr, "Exit returned: %s\n", strerror(errno));
  100.     abort();
  101.     (void) fprintf(stderr, "Abort returned: %s\n", strerror(errno));
  102.     for (;;) {
  103.         continue;
  104.     }
  105.  
  106.     case -1:
  107.     (void) fprintf(stderr, "Fork failed: %s\n", strerror(errno));
  108.     ++errors;
  109.     break;
  110.  
  111.     default:
  112.     while ((w = wait(&ws)) > 0 && w != child) {
  113.         continue;
  114.     }
  115.     if (ws.w_retcode != exitCode) {
  116.         (void) fprintf(stderr, "Exit returned %d, instead of %d.\n",
  117.         ws.w_retcode, exitCode);
  118.         ++errors;
  119.     }
  120.     break;
  121.     }
  122.     return;
  123. }
  124.  
  125.